<?php
require_once ROOT_LIBRARY_UTILITY.DS. 'BaseController.php';
require_once ROOT_LIBRARY_UTILITY.DS. 'Globals.php';

require_once ROOT_APPLICATION_MODULE_BACKEND_MODELS.DS. 'Model_Account.php';
require_once ROOT_APPLICATION_MODULE_BACKEND_FORMS.DS. 'Form_Account.php';
require_once ROOT_APPLICATION_OBJECT.DS. 'Account.php';

class Backend_AccountController extends BaseController
{

    public function init(){
        parent::init();
        $this->_helper->layout->setLayout('backend');
    }

    public function  preDispatch() {
        parent::preDispatch();
        if ($this->view->user->account_types_id != ADMIN)
            $this->_redirect ('/backend/customer');
    }

    public function indexAction(){
        try
    	{
            $this->view->action = "index";
            //select data for pagination
            $mdAccount = new Model_Account();
            $select = $mdAccount->getDataForPagination();
            
            $paginator = Zend_Paginator::factory($select);
            $page = $this->_request->getParam('page') ? $this->_request->getParam('page') : 1;
            $paginator->setCurrentPageNumber($page);
            $paginator->setItemCountPerPage(DEFAULE_ITEMS_IN_LIST);

            $this->view->paginator = $paginator;

            $langSession    = new Zend_Session_Namespace('language');

            if ($this->_request->getMethod () == "POST")
            {
                
            }
            elseif($this->_request->getMethod() == "GET")
            {
                $paramLang = $this->_request->getParam('lang');
                Globals::setLanguage($paramLang);
                Globals::getLanguage($this);
            }
    	}
    	catch (Exception  $ex)
    	{
    	}
    }

    

    public function addAction()
    {
    	try
    	{
            $mdAccount  = new Model_Account();
            $form       = new Form_Account();

            $langSession    = new Zend_Session_Namespace('language');

            if ($this->_request->getMethod () == "POST")
            {
                $form->_requestToForm($this);
                $form->validate(INSERT);

                $form->obj->created_date = date_format(date_create(), 'Y-m-d H:i:s');
                $form->obj->account_types_id = USER;
                $form->obj->password = md5($form->obj->password);

                $mdAccount->_insert($form->obj);
                $this->_redirect('/backend/account');
            }
            elseif($this->_request->getMethod() == "GET")
            {
                $paramLang = $this->_request->getParam('lang');
                Globals::setLanguage($paramLang);
                Globals::getLanguage($this);
            }
    	}
    	catch (Exception  $ex)
    	{
    	}
    }
    

    public function editAction()
    {
        try
    	{
            $mdAccount  = new Model_Account();
            $form       = new Form_Account();

            $hidId = $this->_request->getParam('id');
            $obj = $mdAccount->_getByKey($hidId);

            $langSession    = new Zend_Session_Namespace('language');

            if ($this->_request->getMethod () == "POST")
            {
                $form->_requestToForm($this);
                $form->validate(UPDATE);

                $form->obj->created_date = date("d-m-Y");
                $form->obj->account_types_id = USER;
                if(!empty($form->obj->password))
                    $form->obj->password = md5($form->obj->password);
                else
                    $form->obj->password = $obj->password;

                $mdAccount->_update($form->obj);
                $this->_redirect('/backend/account');
            }
            elseif($this->_request->getMethod() == "GET")
            {
                $this->view->obj = $obj;

                $paramLang = $this->_request->getParam('lang');
                Globals::setLanguage($paramLang);
                Globals::getLanguage($this);
            }
    	}
    	catch (Exception  $ex)
    	{
    	}
    }


    public function deleteAction()
    {
    	$this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        $accountId	= $this->_request->getParam('id');

        if ($this->_request->getMethod() == "POST")
        {
            $mdAccount	= new Model_Account();
            $arrId	= explode(',',$accountId);

            //flag check active accounts exist
            $flag = 0;

            if (count($arrId) >= 1)
                foreach ($arrId as $id)
                {
                    if((int)$id != 0)
                    {
                        $result = $mdAccount->checkAccountActive($id);
                        if((int)$result == 0)
                        {
                            $mdAccount->_delete($id);
                        }
                        else
                        {
                            $flag = 1;
                        }
                    }
                }

            if ($flag == 1)// exist active account while deleting
            {
                if(count($arrId) == 1)
                    echo "Can't delete active account.";
                elseif(count($arrId) > 1)
                    echo "Some active accounts can't be deleted";
            }
        }
    }   
}
?>